package com.wang.controller;

import com.google.common.collect.Lists;
import com.wang.entity.Ips;
import com.wang.entity.Student;
import com.wang.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wangyandong
 * @date 2020/1/26 12:15
 */
@RestController
@RequestMapping("/")
public class StudentController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private StudentService studentService;


    private final ConcurrentHashMap<Integer,String> resultMap = new ConcurrentHashMap<>();

    private final Map<Integer, AtomicInteger> counterMap = new HashMap<>();
    ExecutorService executorService = Executors.newCachedThreadPool();


    private void resetCount(Integer id){
        AtomicInteger atomicInteger = counterMap.getOrDefault(id,new AtomicInteger(5));
        atomicInteger.set(5);
        counterMap.put(id,atomicInteger);
    }

    @GetMapping("/init")
    public void init(Integer id){
        executorService.submit(()->{
            try {
                resetCount(id);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                resultMap.put(id,simpleDateFormat.format(new Date()));
                while (counterMap.get(id).decrementAndGet()>0){
                    Thread.sleep(5000);
                    resultMap.put(id,simpleDateFormat.format(new Date()));
                }
            }catch (Exception e){

            }
            finally {
                counterMap.remove(id);
                resultMap.remove(id);
            }

        });
    }

    @GetMapping("getR")
    public String getR(Integer id){
        resetCount(id);
        return resultMap.get(id);
    }

    @GetMapping("/get")
    public Student getById(Integer id) throws InterruptedException {
        logger.info("参数id = {}",id);


        return studentService.getById(id);
    }

    private String gen(){
        logger.info("==================gen=======================");
        return "hello world";
    }

    @GetMapping("getips")
    public List<Ips> getIps(){
        System.out.println("------------------------------------------------------");
        Ips ips = new Ips();
        ips.setTargets(Lists.newArrayList("10.193.108.97:8222"));
        Map<String,String> labels= new HashMap<>();
        labels.put("__meta_prometheus_job","boot");
        ips.setLabels(labels);
        return Lists.newArrayList(ips);
    }

    @GetMapping("/test")
    public String test(){
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+" ===============");
        return "ok";
    }

    @GetMapping("/getEnv")
    public String getEnv(@RequestParam("key") String key){
        return System.getenv(key);
    }

}
